package com.car.car.converter;


import com.car.car.dataobject.OrderDetail;
import com.car.car.dto.OrderDTO;
import com.car.car.enums.ResultEnums;
import com.car.car.exception.SellException;
import com.car.car.form.OrderForm;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class OrderForm2OrderDTOConverter {



   public static OrderDTO convert(OrderForm orderForm){
        Gson gson = new Gson();
        OrderDTO orderDTO = new OrderDTO();

        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());

        List<OrderDetail> orderDetailList = new ArrayList<>();


        try {
           orderDetailList =  gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){
            log.error("orderItemError={}",orderForm.getItems());
            throw new SellException(ResultEnums.PARAM_ERROR);
        }

       orderDTO.setOrderDetailList(orderDetailList);
       return orderDTO;
   }


}
